package tools

import (
	"go-base/hs-gin/log"
	"strconv"
)

func InterfaceToInt(data interface{}) int {
	var (
		v int
		err error
	)
	switch data.(type) {
	case []byte:
		v, err = strconv.Atoi(string(data.([]byte)))
	case string:
		v, err = strconv.Atoi(data.(string))
	}
	if err != nil {
		log.Logger.Error("InterfaceToInt=>Atoi error=>", err.Error())
		return 0
	}
	return v
}

func InterfaceToInt64(data interface{}) int64 {
	var (
		v int64
		err error
	)
	switch data.(type) {
	case []byte:
		v, err = strconv.ParseInt(string(data.([]byte)), 0, 64)
	case string:
		v, err = strconv.ParseInt(data.(string), 0, 64)
	}
	if err != nil {
		log.Logger.Error("InterfaceToInt=>ParseInt error=>", err.Error())
		return 0
	}
	return v
}

func InterfaceToFloat(data interface{}) float64 {
	var (
		v float64
		err error
	)
	switch data.(type) {
	case []byte:
		v, err = strconv.ParseFloat(string(data.([]byte)), 0)
	case string:
		v, err = strconv.ParseFloat(data.(string), 0)
	}
	if err != nil {
		log.Logger.Error("InterfaceToInt=>ParseFloat error=>", err.Error())
		return 0
	}
	return v
}
